local jass = require 'jass.common'
local slk = require 'jass.slk'
local japi = require 'jass.japi'
local mt = ac.unit.__index



mt.level = 1

function mt:set_level(level,show)
    level = math.min(level,self.max_level)
	local lv = self.level 
	local add = level - lv
	if level ~= lv then 
		for i = lv+1, level do 
			self.level = i
			self:notify('单位-英雄升级', self)
		end 
	end 

	self.level = level
	--SetHeroLevelBJ(self.handle,self.level,show or false)
	self.exp = self.exp_need[level] or 0
    self:add_effect([[Abilities\Spells\Other\Levelup\LevelupCaster.mdl]]):remove()

	if self.update_attribute then
		for k,v in pairs(self.update_attribute) do
			self:add(k,v*add)
		end
	end
end


function mt:add_level(level,show)
	self:set_level(self.level + level,show or false)
end

function mt:get_level()
	--self.level = jass.GetHeroLevel(self.handle)
	return self.level
end



--经验
mt.exp = 0
--到下级需要的经验
mt.exp_need = {}
mt.max_level = 100

local data = load_excel('基础设定.xlsx','经验设置')
local sum = 0
for a=2,mt.max_level do
    -- local lv = math.ceil(a/10)
    -- sum = sum + (lv+1)*lv/2*1000
    local da = data[a-1]
    mt.exp_need[a] = da['总经验需求']
end

local NeedHeroXP = slk['misc']['Misc']['NeedHeroXP']
if NeedHeroXP then
    --for i, v in ipairs(ac.split(NeedHeroXP, ',')) do 
    --    mt.exp_need[i] = tonumber(v)
    --end 
end

function mt:add_exp(value)
	if not self.max_level then
		return
	end
	if self:get_level() >= self.max_level then
		return
	end
	self.exp = self.exp + value
	self.exp = math.min(self.exp, self.exp_need[self.max_level])
	--jass.SetHeroXP(self.handle, self.exp, false)
    while(self.exp_need[self.level+1]) do
        if self.exp >= self.exp_need[self.level+1] and self.level + 1 <= self.max_level then
            self:add_level(1)
        else
            break
        end
    end
end




--创建模板单位
function mt:create_temp_unit(name,point,angle)
    return self.owner:create_temp_unit(name,point,angle)
end